/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Wavelet;

/**
 *
 * @author FoSa
 */
public class haar {

    public static int[] invoke(int[] input) {

        //This function assumes input.length=2^n, n>1
        int[] output = new int[input.length];

        for (int length = input.length >> 1;; length >>= 1) {
//            System.out.println(length);
            //length=2^n, WITH DECREASING n
            for (int i = 0; i < length; i++) {
                int sum = input[i * 2] + input[i * 2 + 1];
                int difference = input[i * 2] - input[i * 2 + 1];
                output[i] = sum;
                output[length + i] = difference;
            }
            if (length == 1) {
                return output;
            }

            //Swap arrays to do next iteration
            System.arraycopy(output, 0, input, 0, length << 1);
        }
    }

    public static void main(String args[]) {
        int[] sinal = new int[]{0, 1, 0, -1, 2, 3, 1, 3, 2, 3};
        int[] output = invoke(sinal);
        for (int i = 0; i < sinal.length; i++) {
            int j = output[i];
            System.out.print(j + " : ");
        }
        System.out.println("");
        int base = 9;
        int exp = 1;
        int result = base >> exp;
        System.out.println("base: " + base);
        System.out.println("exp: " + exp);
        System.out.println("Result: " + result);

    }
}
